package com.jerry.huihome.module.home.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.home.adapter.PullLiveLoreInfoAdapter;
import com.jerry.huihome.module.home.entity.Live;
import com.jerry.huihome.utils.OkHttpUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
;

/**
 * Created by LGuipeng on 2016/5/12.
 */
public class LiveAllFragment extends BaseFragment{
    protected ImageView imageViewBg;
    @BindView(R.id.pull_lv_detail)
    protected PullToRefreshListView mLore;
    @BindView(R.id.rp_live)
    protected RadioGroup rpLive;
    @BindView(R.id.ll_live_noFound)
    LinearLayout linearLayout;
    List<Live.BodyBean.DynaItemsBean> dynaItemsBeenList;
    private String cate;
    int index = 1;
    int step = -1;
    String ADDRESS = "&uid=1721&pageSize=10&pageIndex=";
    private PullLiveLoreInfoAdapter pullLvAdapter;
    private final ImageLoader imageLoader;
    private final DisplayImageOptions options;

    public LiveAllFragment(){
        imageLoader = BaseApplication.getApplication().getImageLoader();
        options = BaseApplication.getApplication().getImageOptions();
        dynaItemsBeenList = new ArrayList<>();
    }

    protected int getLayoutId(){
        return R.layout.vp_fragment_live;
    }

    @Override
    protected void getReqParams(Bundle bundle){
        cate = bundle.getString("cate");
    }

    @Override
    protected void initViews(){
        if(dynaItemsBeenList.size() == 0){
            linearLayout.setVisibility(View.VISIBLE);
        }
        pullLvAdapter = new PullLiveLoreInfoAdapter(getActivity(), dynaItemsBeenList);
        mLore.setAdapter(pullLvAdapter);
        mLore.getRefreshableView().setSelector(android.R.color.transparent);
        mLore.setMode(PullToRefreshBase.Mode.BOTH);
        mLore.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>(){
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView){
                index = 1;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView){
                index++;
                loadData();
            }
        });
        rpLive.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId){
                int count = group.getChildCount();
                for(int i = 0; i < count; i++){
                    if(rpLive.getChildAt(i).getId() == checkedId){
                        step = i - 1;
                        mLore.getRefreshableView().setSelection(0);
                        loadData();
                        break;
                    }
                }
            }
        });
    }

    @Override
    protected void loadData(){
        final String Url = Urls.LIVE_URL + "cate=" + cate + "&step=" + step + ADDRESS + index;
        OkHttpUtils.get(Url, new OkHttpUtils.OnGetDataListener(){
            @Override
            public void onResponse(String url, String data){
                mLore.onRefreshComplete();
                if(index == 1){
                    dynaItemsBeenList.clear();
                }
                if(url.equals(Url)){
                    Gson gson = new Gson();
                    Live live = gson.fromJson(data, Live.class);
                    if(live != null){
                        dynaItemsBeenList.addAll(live.getBody().getDynaItems());
                        getBgPic(live.getBody().getBannerImg());
                    }
                    pullLvAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(String url, String error){
                mLore.onRefreshComplete();
            }
        });
    }

    private void getBgPic(String bannerImg){
        if(imageViewBg != null){
            mLore.getRefreshableView().removeHeaderView(imageViewBg);
        }
        imageViewBg = new ImageView(getActivity());
        imageViewBg.setLayoutParams(
                new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                        (int)(getContext().getResources().getDisplayMetrics().density*200 + 0.5f)));
        imageLoader.displayImage(bannerImg, imageViewBg, options);
        mLore.getRefreshableView().addHeaderView(imageViewBg);
    }
}
